import { AuthGuard } from '@nestjs/passport';
import { ApiResult, config } from 'src/config';
import {
  Controller,
  Post,
  UploadedFile,
  UploadedFiles,
  UseGuards,
  UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';

@Controller('upload')
export class UploadController {
  @Post('file')
  @UseInterceptors(FileInterceptor('file'))
  uploadFile(@UploadedFile() file: Express.Multer.File): ApiResult {
    console.log(file);
    return ApiResult.SUCCESS({
      filename: file.filename,
      url: `${config.host}/${config.uploadPath}/` + file.filename,
    });
  }

  @Post('files')
  @UseInterceptors(FileInterceptor('files'))
  uploadFiles(@UploadedFiles() files: Array<Express.Multer.File>): ApiResult {
    const fileList = files.map((file) => ({
      filename: file.filename,
      url: `${config.host}/${config.uploadPath}/` + file.filename,
    }));
    return ApiResult.SUCCESS(fileList);
  }
}